//Shop implementation

#include "shop.h"
#include <iostream>
using namespace std;

shop::shop(){
               shopping_array = new Customer_Obj[1000];
               min = 0;
               max = 0;
               maxShop = 0;
               numProcessed = 0;
}
shop::shop(sync *sync_object, control *control_object, sweeps *sweeps_object, Deli *deli_object){
                shopping_array = new Customer_Obj[1000];
                sync_obj = sync_object;
                control_obj = control_object;
                sweeps_obj = sweeps_object;
                deli_obj = deli_object;
                min = max = numProcessed = maxShop = 0;
}

void shop::start_shopping(){
     cout << "Shopping Started." << endl;
    Time time_conv;
    while(((*control_obj).peek(*sync_obj)).getArrivalTime() == (*sync_obj).return_time() && !((*control_obj).peek(*sync_obj)).isInDeli() && !((*control_obj).peek(*sync_obj)).isInSweepstakes()){
         cout << "While loop started." << endl;
         shopping_array[max] = (*control_obj).empty2store((*sync_obj)); cout << "EMPTIED";
         if(time_conv.round2minutes(shopping_array[max].getNumItems() * 11) > maxShop)
                                                                        maxShop = time_conv.round2minutes(shopping_array[max].getNumItems() * 11);
         shopping_array[max].setActionTime(time_conv.round2minutes(shopping_array[max++].getNumItems() * 11));
         action = true;
         cout << "CONTROL";
         }
    cout << "While exited." << endl;
    if(((*deli_obj).peek()).getActionTime() == (*sync_obj).return_time() && ((*deli_obj).peek()).getFirstName() != ""){
         cout << "If started." << endl;
         shopping_array[max] = (*deli_obj).remove(); cout << "EMPTIED";
         if(time_conv.round2minutes(shopping_array[max].getNumItems() * 11) > maxShop)
                                                                        maxShop = time_conv.round2minutes(shopping_array[max].getNumItems() * 11);
         shopping_array[max].setActionTime(time_conv.round2minutes(shopping_array[max++].getNumItems() * 11));
         action = true;
         cout << "DELI";
         }
    cout << "If1 exited." << endl;
    if(((*sweeps_obj).peek()).getActionTime() == (*sync_obj).return_time() && !((*sweeps_obj).peek()).isInDeli() && ((*sweeps_obj).peek()).getFirstName() != ""){
         cout << "If2 Started." << endl;
         shopping_array[max] = (*sweeps_obj).register_dequeue(); cout << "EMPTIED";
         if(time_conv.round2minutes(shopping_array[max].getNumItems() * 11) > maxShop)
                                                                        maxShop = time_conv.round2minutes(shopping_array[max].getNumItems() * 11);
         shopping_array[max].setActionTime(time_conv.round2minutes(shopping_array[max++].getNumItems() * 11));
         action = true;
         cout << "SWEEPS" << endl;
         }
    cout << "All done emptying." << endl;    
    if(action){
               shopTimeAvg[numProcessed] = time_conv.round2minutes(shopping_array[max].getNumItems()); cout << "Average Time Array updated.";
               numProcessed++; cout << "Number Processed Updated." << endl;\
               action = false;
    }
}

Customer_Obj shop::peek(){
     Customer_Obj empty_customer;
     for(int i = min; i < max; i++){
            if (shopping_array[i].getActionTime() == (*sync_obj).return_time()){
                                                  return(shopping_array[i]);
            }
                                                  
     }
     return(empty_customer);
}

Customer_Obj shop::go2checkout(){
     Customer_Obj empty_customer;
     Customer_Obj temp_customer;
     for(int i = min; i < max; i++){
            if (shopping_array[i].getActionTime() == (*sync_obj).return_time()){
                                                  temp_customer = shopping_array[i];
                                                  shopping_array[i] = empty_customer;
                                                  if(i == min)
                                                       min++;
                                                  return(shopping_array[i]);
            }
     }
     return(empty_customer);
}

void shop::show_stats(){
     int temp = 0;
     for(int i = 0; i < numProcessed; i++){
             if(maxShop < shopTimeAvg[i])
                        maxShop = shopTimeAvg[i];
             temp += shopTimeAvg[i];
     }
     avgShop = temp / numProcessed;        
     Time maxShopT(maxShop);
     Time avgShopT(avgShop);
     cout << "--Shopping--" << endl;
     cout << numProcessed << " Customers Processed" << endl;
     cout << "Maximum Shop Time: "; maxShopT.dispTime(); cout << endl;
     cout << "Average Shop Time: "; avgShopT.dispTime(); cout << endl;
}
